package com.action;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.ResponseBean;
import com.pojo.Admin;
import com.pojo.MovieList;
import com.service.DyService;
import com.util.HardUtil;
import com.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.*;

@Controller
@RequestMapping("/system")
public class SystemAction {

    @Value("${server.url}")
    private String serer;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${dy.home}")
    private String home;

    @Autowired
    private DyService dyService;



    /**
     * 获取配置文件ip地址
     * @param request
     * @return
     */

    @RequestMapping("/getIP")
    @ResponseBody
    public ResponseBean getIP(HttpServletRequest  request){
        Map map = new HashMap();
        //获取配置文件中的IP地址
        try {
            InputStream is = new FileInputStream("ip.properties");
            Properties p = new Properties();
            p.load(is);
            String ip = p.getProperty("server.ip");
            map.put("ip",ip);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取本机所有IP地址
        List list = IPUtil.getLocalIpAddr();
        map.put("ips",list);

        return new ResponseBean(200,"成功",map);


    }

    /**
     * 获取下载时间
     * @param request
     * @return
     */
    @RequestMapping("/getDownTime")
    @ResponseBody
    public ResponseBean getDownTime(HttpServletRequest  request){
        Map map = new HashMap();
        try {
            InputStream is = new FileInputStream("time.properties");
            Properties p = new Properties();
            p.load(is);
            String beginTime = p.getProperty("begin");
            String endTime = p.getProperty("end");
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ResponseBean(200,"成功",map);


    }


    @RequestMapping("/saveDownTime")
    @ResponseBody
    public ResponseBean saveDownTime(HttpServletRequest  request,String beginTime,String endTime) throws Exception {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"非法访问",null);
        }
        OutputStream out = new FileOutputStream("time.properties");
        out.write(("begin="+beginTime).getBytes());
        out.write(("\r\n").getBytes());
        out.write(("end="+endTime).getBytes());
        out.close();
        return new ResponseBean(200,"修改成功",null);
    }




    /**
     * 保存序列号
     * @param request
     * @param seri
     * @param mainBoard
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveSeri")
    @ResponseBody
    public JSONObject saveSeri(HttpServletRequest  request,String seri,String mainBoard) throws Exception {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        System.out.println(admin);
        if (admin == null) {
           JSONObject jsonObject = new JSONObject();
           jsonObject.put("code",403);
           jsonObject.put("msg","请登录先");
           return jsonObject;
        }
        String url = serer+"api/v1/saveShouQuan?mainboardId={1}&seri={2}";
        String result = restTemplate.getForObject(url,String.class,mainBoard,seri);
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject;
    }

    @RequestMapping("/saveIP")
    @ResponseBody
    public ResponseBean saveIP(HttpServletRequest  request,String ip) throws Exception {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"非法访问",null);
        }
        OutputStream out = new FileOutputStream("ip.properties");
        out.write(("server.ip="+ip).getBytes());
        out.close();
        return new ResponseBean(200,"修改成功",null);
    }

    @RequestMapping("/savePMZ")
    @ResponseBody
    public ResponseBean savePMZ(HttpServletRequest  request,String pmz) throws Exception {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"非法访问",null);
        }
        if (pmz==null){
            pmz = "";
        }
        OutputStream out = new FileOutputStream("pmz.properties");
        out.write(("pmz="+pmz).getBytes());
        out.close();
        return new ResponseBean(200,"修改成功",null);
    }


    @RequestMapping("/removeDy")
    @ResponseBody
    public ResponseBean removeDy(HttpServletRequest  request){
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"非法访问",null);
        }

        //删除数据库里面下架电脑
        dyService.removeDownDy();

        File file = new File(home+"dy");
        int count = 0;
        if (file.exists() && file.isDirectory())
        {
            File[] files = file.listFiles();
            for (File f:files)
            {
                if (f.isFile())
                {
                    String name = f.getName();
                    QueryWrapper q = new QueryWrapper();
                    q.eq("md5",name);

                    MovieList movieList = dyService.getOne(q);
                    if (movieList==null)
                    {
                        count++;
                        f.delete();
                    }
                }
            }
        }
        return new ResponseBean(200,"清理成功",count);
    }


    @RequestMapping("/getPMZ")
    @ResponseBody
    public ResponseBean getPMZ(HttpServletRequest  request){
        try {
            InputStream is = new FileInputStream("pmz.properties");
            Properties p = new Properties();
            p.load(is);
            String pmz = p.getProperty("pmz");
            pmz = new String(pmz.getBytes("iso8859-1"),"utf-8");
            is.close();
            return new ResponseBean(200,"成功",pmz);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseBean(404,"获取失败",null);
        }




    }




    @RequestMapping("/getSeri")
    @ResponseBody
    public ResponseBean getSeri(HttpServletRequest  request) throws Exception {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"非法访问,请重新登录",null);
        }
        String id = HardUtil.getMainBorId();
        Map map = new HashMap();
        map.put("mainBoradId",id); //主板序列号

        String url = serer+"api/v1/getShouquan?mainboardId={1}";
        String result = restTemplate.getForObject(url,String.class,id);
        JSONObject  jsonObject =  JSONObject.parseObject(result);
        map.put("system",jsonObject);
        if (jsonObject!=null) {
            Long daoqi = jsonObject.getLong("dy_shouquan_daoqi");
            if (daoqi != null && daoqi < System.currentTimeMillis()) {
                map.put("daoqi", true);
            } else {
                map.put("daoqi", false);
            }
        }
        return new ResponseBean(200,"获取成功",map);
    }





}
